package com.neusoft.demo.filter;

import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author zhouQF
 * @description 过滤器
 * @date 2019/9/17
 */
@Component
@WebFilter(filterName = "myFilter",urlPatterns = "/*")
public class MyFilter implements Filter {

    String[] defaultUrls = {"/index", "/test", "/demo"};

    @Override
    public void init(FilterConfig filterConfig) {
        System.out.println("filter init...");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("filter doFilter...");
        HttpServletRequest hsr = (HttpServletRequest)request;
        String url  = hsr.getServletPath();
        for(String str : defaultUrls){
            if(str.equals(url)){
                chain.doFilter(request, response);
            }
        }

        //chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        System.out.println("filter destroy...");
    }
}
